package com.ms.p2p.plugin.payment.fengfu;


import com.ms.common.mapper.JsonMapper;
import com.ms.p2p.Constants;
import com.ms.p2p.entity.account.AccountRecharge;
import com.ms.p2p.entity.system.PayMethod;

import com.ms.p2p.plugin.payment.AbstractPaymentPlugin;
import com.ms.p2p.plugin.payment.IPaymentEvent;
import com.ms.p2p.plugin.payment.fengfu.util.SignatureUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class FengfuPayment extends AbstractPaymentPlugin implements IPaymentEvent {


    @Override
    public void register() {

    }

    @Override
    public String onPay(Integer userId, BigDecimal money, HashMap other,HttpServletRequest request) throws Exception {
        PayMethod payment = payMethodService.findById(getId());
        AccountRecharge rechargeOrder = BuildReCharge(userId, money, payment);
        String requestId = rechargeOrder.getTrade_no();
        String tradeProcess = payMethodValueService.findByName("fengfu.tradeProcess").getParamValue();
        String totalBizType = payMethodValueService.findByName("fengfu.totalBizType").getParamValue();
        String totalPrice = rechargeOrder.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
        String backurl = payMethodValueService.findByName("fengfu.backurl").getParamValue();
        String noticeurl = payMethodValueService.findByName("fengfu.noticeurl").getParamValue();
        String key = payMethodValueService.findByName("fengfu.md5key").getParamValue();
        String weburl = settingService.findValueByCode("weburl");
        String urlStr = "http://" + weburl + "";
        noticeurl = urlStr + noticeurl;
        backurl = urlStr + backurl;
        String md5Str = requestId + tradeProcess + totalBizType + totalPrice + backurl + backurl + noticeurl;
        String mersignature = SignatureUtil.hmacSign(md5Str, key);
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("requestId", requestId);
        map.put("tradeProcess", tradeProcess);
        map.put("totalBizType", totalBizType);
        map.put("totalPrice", totalPrice);
        map.put("backurl", backurl);
        map.put("returnurl", backurl);
        map.put("noticeurl", noticeurl);
        map.put("mersignature", mersignature);
        map.put("productId", requestId);
        map.put("productName", "invest");
        map.put("fund", totalPrice);
        map.put("merAcct", tradeProcess);
        map.put("bizType", totalBizType);
        map.put("productNumber", "1");
        logger.info("网银充值请求："+ JsonMapper.toJsonString(map));
        return buildRequest(map, "https://www.sumapay.com/sumapay/unitivepay_bankPayForNoLoginUser?");
    }

    @Override
    public String onReturn(HttpServletRequest request) throws Exception {
        String requestId = request.getParameter("requestId");
        String payId = request.getParameter("payId");
        String fiscalDate = request.getParameter("fiscalDate");
        String description = request.getParameter("description");
        String resultSignature = request.getParameter("resultSignature");
        String md5Signstr = requestId + payId + fiscalDate + description;
        String md5Sign = SignatureUtil.hmacSign(md5Signstr, payMethodValueService.findByName("fengfu.md5key").getParamValue());
        String bankCode = request.getParameter("bankCode");
        String tradeAmount = request.getParameter("tradeAmount");
        String status = request.getParameter("status");
        logger.info("网银充值回调开始："+ JsonMapper.toJsonString(request.getParameterMap()));
        if (StringUtils.isNotBlank(md5Sign) && StringUtils.equals(md5Sign,resultSignature)) {
            String result =  OnlineReturn(requestId, "在线网银充值-丰付支付");
            if(Constants.SUCCESS.equals(result)){
                logger.info("网银充值回调成功："+ JsonMapper.toJsonString(request.getParameterMap()));
            }
            return result;
        } else {
            return Constants.VALIDATEFAIL;
        }

    }

    @Override
    public String getTradeNo(Map<String, String> params) throws Exception {
        return null;
    }

    @Override
    public String getType() {
        return "1";
    }

    @Override
    //id需要与paymethod id一样
    public String getId() {
        return "53";
    }

    @Override
    //名称需要与包明一样
    public String getName() {
        return "fengfu";
    }

    @Override
    public String getVersion() {
        return "1.0.0";
    }

    @Override
    public String getAuthor() {
        return "daimajia";
    }

    @Override
    public void perform(Object... params) {

    }
}
